# Date- 5/3/23
# Script No. 3 
# File target: 1) Creating Figures 2-10


library(tidyverse)
library(ggplot2)
library(ggpubr)
library(cowplot)

setwd("C:/Users/yaira/Desktop/ideological polarization/FINAL/Tables and Graphs")

###################################################################################################################
################################################# (1) Figure 2  ###################################################
###################################################################################################################

###################################### four graphs ################################################
### one divided graph
x <- c(1, 2, 1, 3)
y <- c(1, 4, 1, 4)
act <- c(1,1,2,2)
pv <- data.frame(x,y, act)
pe.wa <- 
  pv %>%
  filter(act==1) %>%
  ggplot(aes(x,y)) +
  geom_point(size = 2) +
  ggtitle("Weak activists") +
  labs(x = "", y = "Party extremity") +
  scale_x_continuous(limits = c(0,4), breaks = c(0, 1, 2, 3, 4), 
                     labels = c("Center", "", "", "", "Extreme")) +
  scale_y_continuous(limits = c(0,5), breaks = c(1, 4)) +
  
  geom_vline(xintercept=0, color = "black") +                
  
  geom_segment(aes(x = 0, y = 4, xend = 1.9, yend = 4),                         # moderate party
               arrow = arrow(length = unit(0.02, "npc"), type = "closed")) +
  geom_segment(aes(x = 0, y = 1, xend = 0.9, yend = 1),                       
               arrow = arrow(length = unit(0.02, "npc"), type = "closed")) +
  geom_segment(aes(x = 1, y = 1, xend = 1, yend = 4), linetype = "dashed") +
  
  geom_curve(aes(x = 1, y = 1, xend = 1.9, yend = 3.8),                             
             arrow = arrow(length = unit(0.03, "npc")), 
             col = "grey70", curvature = 0.1) +
  theme_bw() +
  theme(panel.border = element_blank(),
        panel.grid.minor = element_blank(),
        plot.title = element_text(size=10, face = "bold", hjust = 0.5),
        axis.title.y = element_text(size=10, face = "bold", hjust = 0.5),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank()) 
pe.wa
ggsave("argunet model - PE1.png")

pe.sa <- 
  pv %>% 
  filter(act==2) %>% 
  ggplot(aes(x,y)) +
  geom_point(size = 2) +
  labs(x = "", y = "", title = "Strong activists") +
  scale_x_continuous(limits = c(0,4), breaks = c(0, 1, 2, 3, 4), 
                     labels = c("Center", "", "", "", "Extreme")) +
  scale_y_continuous(limits = c(0,5), breaks = c(1, 4), 
                     labels = c("Voters'\nposition", "Party\nposition"), position = "right") +
  
  geom_vline(xintercept=0, color = "black") +
  geom_segment(aes(x = 0, y = 4, xend = 2.9, yend = 4),                     # extreme party  
               arrow = arrow(length = unit(0.02, "npc"), type = "closed")) +
  geom_segment(aes(x = 0, y = 1, xend = 0.9, yend = 1),                       
               arrow = arrow(length = unit(0.02, "npc"), type = "closed")) +
  geom_segment(aes(x = 1, y = 1, xend = 1, yend = 4), linetype = "dashed") +
  
  geom_curve(aes(x = 1, y = 1, xend = 2.9, yend = 3.8),                             
             arrow = arrow(length = unit(0.03, "npc")), 
             col = "grey30", curvature = 0.1) +
  theme_bw() +
  theme(plot.title = element_text(size = 10, hjust = 0.5, face = "bold"),
        panel.border = element_blank(),
        panel.grid.minor = element_blank()) 
pe.sa
ggsave("argunet model - PE2.png")

### party polarization###
x <- c(2.5, 4, 6, 7.5, 1.25, 4, 6, 8.75)
y <- c(4, 1, 1, 4, 4, 1, 1, 4)
act <- c(1, 1, 1, 1, 2, 2, 2, 2)
pp <- data.frame(x,y, act)
pp.wa <- 
  pp %>%
  filter(act==1) %>%
  ggplot(aes(x,y)) +
  geom_point(size = 2) +
  labs(x = "", y = "Party polarization") +
  scale_x_continuous(limits = c(0,10), breaks = c(0, 2.5, 5, 7.5, 10), 
                     labels = c("Left", "", "Center", "", "Right")) +
  scale_y_continuous(limits = c(0,5), breaks = c(1, 4)) +
  
  geom_vline(xintercept=5, color = "black") +                
  
  geom_segment(aes(x = 5.05, y = 4, xend = 2.7, yend = 4),                         # moderate party
               arrow = arrow(length = unit(0.02, "npc"), type = "closed")) +
  geom_segment(aes(x = 5.05, y = 1, xend = 4.2, yend = 1),                       
               arrow = arrow(length = unit(0.02, "npc"), type = "closed")) +
  geom_segment(aes(x = 4, y = 1, xend = 4, yend = 4), linetype = "dashed", 
               col = "black") +
  geom_curve(aes(x = 4, y = 1, xend = 2.6, yend = 3.8),                             
             arrow = arrow(length = unit(0.03, "npc")), 
             col = "grey70", curvature = -0.1) +
  
  geom_segment(aes(x = 5.05, y = 4, xend = 7.3, yend = 4),                         # moderate party
               arrow = arrow(length = unit(0.02, "npc"), type = "closed")) +
  geom_segment(aes(x = 5.05, y = 1, xend = 5.8, yend = 1),                       
               arrow = arrow(length = unit(0.02, "npc"), type = "closed")) +
  geom_segment(aes(x = 6, y = 1, xend = 6, yend = 4), linetype = "dashed", 
               col = "black") +
  geom_curve(aes(x = 6, y = 1, xend = 7.4, yend = 3.8),                             
             arrow = arrow(length = unit(0.03, "npc")), 
             col = "grey70", curvature = 0.1) +
  
  theme_bw() + 
  theme(axis.title.y = element_text(size=10, hjust = 0.5, face = "bold"),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        panel.border = element_blank(),
        panel.grid.minor = element_blank()) 

pp.wa
ggsave("argunet model - PP1.png")

## party polarization with strong activists
pp.sa <- 
  pp %>%
  filter(act==2) %>%
  ggplot(aes(x,y)) +
  geom_point(size = 2) +
  labs(x = "", y = "") +
  scale_x_continuous(limits = c(0,10), breaks = c(0, 2.5, 5, 7.5, 10), 
                     labels = c("Left", "", "Center", "", "Right")) +
  scale_y_continuous(limits = c(0,5), breaks = c(1, 4), 
                     labels = c("Voters'\nposition", "Party\nposition"), position = "right") +
  
  geom_vline(xintercept=5, color = "black") +                
  
  geom_segment(aes(x = 5.05, y = 4, xend = 1.45, yend = 4),                         # moderate party
               arrow = arrow(length = unit(0.02, "npc"), type = "closed")) +
  geom_segment(aes(x = 5.05, y = 1, xend = 4.2, yend = 1),                       
               arrow = arrow(length = unit(0.02, "npc"), type = "closed")) +
  geom_segment(aes(x = 4, y = 1, xend = 4, yend = 4), linetype = "dashed", 
               col = "black") +
  geom_curve(aes(x = 4, y = 1, xend = 1.35, yend = 3.8),                             
             arrow = arrow(length = unit(0.03, "npc")), 
             col = "gray30", curvature = -0.1) +
  
  geom_segment(aes(x = 5.05, y = 4, xend = 8.55, yend = 4),                         # moderate party
               arrow = arrow(length = unit(0.02, "npc"), type = "closed")) +
  geom_segment(aes(x = 5.05, y = 1, xend = 5.8, yend = 1),                       
               arrow = arrow(length = unit(0.02, "npc"), type = "closed")) +
  geom_segment(aes(x = 6, y = 1, xend = 6, yend = 4), linetype = "dashed", 
               col = "black") +
  geom_curve(aes(x = 6, y = 1, xend = 8.65, yend = 3.8),                             
             arrow = arrow(length = unit(0.03, "npc")), 
             col = "gray30", curvature = 0.1) +
  theme_bw() +
  theme(panel.border = element_blank(),
        panel.grid.minor = element_blank()) 
pp.sa
ggsave("argunet model - PP2.png")

ggdraw() +
  draw_plot(pp.wa, x = 0, y = 0, width = .5, height = 0.5) +
  draw_plot(pe.wa, x = 0, y = .5, width = .49, height = 0.5) +
  draw_plot(pp.sa, x = .5, y = 0, width = .5, height = 0.5) +
  draw_plot(pe.sa, x = .51, y = .5, width = .49, height = 0.5) 


ggsave("argunet model.png", width = 7)


#########################################################################################################
############################################# (2) Figure 3  #############################################
#########################################################################################################


# in order to highlight Isr and UK
isr <- ppdb.fin %>%
  filter(COUNTRY=="Israel")
grb <- ppdb.fin %>%
  filter(COUNTRY=="United Kingdom")

# adding labels
ppdb.fin$labels.i.b <- NA
ppdb.fin$labels.i.b[which(ppdb.fin$COUNTRY == "United Kingdom")] <- "UK"
ppdb.fin$labels.i.b[which(ppdb.fin$COUNTRY == "Israel")] <- "ISR"


# scatter
ppdb.act <- ppdb.fin %>%
  drop_na(pfam) 

ggplot(ppdb.act, aes(factor(pfam), activists)) +
  geom_point(size = 2) +
  geom_point(data=isr, aes(x = factor(pfam), y = activists), 
             color='grey60', size=2) +
  geom_point(data=grb, aes(x = factor(pfam), y = activists), 
             color='grey80',  size=2) +
  labs(x = "", y = "Activists relative power ") +
  geom_text(label = ppdb.act$labels.i.b, nudge_x = 0.25, check_overlap = T, size = 3.25) +
  theme_bw() +
  theme(plot.title = element_text(size = 15),
        axis.title.x = element_text(size = 10),
        axis.title.y = element_text(size = 10),
        panel.border = element_blank(),
        axis.line = element_line(size = 0.5, linetype = "solid",
                                 colour = "black")) +
  scale_x_discrete(breaks = 0:7, labels=c("Left\nSocialists","Greens","Social\nDemocrats",
                                          "Agrarian","Liberals","Christian\nDemocrats",
                                          "Conservative", "Far Right")) +
  scale_y_continuous(limits = c(0,1.2), breaks = c(0, 0.5, 1)) 

ggsave("activists.scatter.png", width = 7, height = 4)


#########################################################################################################
############################################# (3) Figure 4  #############################################
#########################################################################################################

#### polarization at the party level CSES IMD ###

setwd("C:/Users/yaira/Desktop/ideological polarization/FINAL/Do and Data")
cses.imd <- read_csv("cses_imd.csv")

# Dalton formula (2008)
#4                              #3                #1                  #2
# party polarization = sqtr(sum((party vote share)*([party position - country mean position]/5)^2))

# 1
### party position ###
## Creating var of party position by the mean of voters' perceived party placement
cses.imd$party.position <- NA

# creating var for vote choice
cses.imd$voted.for <- NA

for (i in 1:nrow(cses.imd)){
  if (cses.imd$IMD3002_PR_1[i] <9000000){
    cses.imd$voted.for[i] <- cses.imd$IMD3002_PR_1[i]
  } else if (cses.imd$IMD3002_LH_PL[i] <9000000){
    cses.imd$voted.for[i] <- cses.imd$IMD3002_LH_PL[i]
  } else if (cses.imd$IMD3002_LH_DC[i] <9000000){
    cses.imd$voted.for[i] <- cses.imd$IMD3002_LH_DC[i]
  }
}

summary(cses.imd$voted.for)
describe(cses.imd$voted.for)

cses.imd.country <- cses.imd %>% drop_na(voted.for)

# amendments: in germany voted for var (IMD3002_LH_PL)  refers to CDU as 2760001 
# and party placement (IMD3007_A) as 2760002 (see IMD5000_A).
cses.imd.country$IMD5000_A[which(cses.imd.country$IMD1006_NAM=="Germany")] <- 2760001

# creating var for voters' perceived party placement
cses.imd.country$voted.for.score <- NA

for (i in 1:nrow(cses.imd.country)){
  score <- cses.imd.country$voted.for[i]
  if (cses.imd.country$IMD5000_A[i] == score){
    cses.imd.country$voted.for.score[i] <- cses.imd.country$IMD3007_A[i]
  } else if (cses.imd.country$IMD5000_B[i] == score){
    cses.imd.country$voted.for.score[i] <- cses.imd.country$IMD3007_B[i]
  } else if (cses.imd.country$IMD5000_C[i] == score){
    cses.imd.country$voted.for.score[i] <- cses.imd.country$IMD3007_C[i]
  } else if (cses.imd.country$IMD5000_D[i] == score){
    cses.imd.country$voted.for.score[i] <- cses.imd.country$IMD3007_D[i]
  } else if (cses.imd.country$IMD5000_E[i] == score){
    cses.imd.country$voted.for.score[i] <- cses.imd.country$IMD3007_E[i]
  } else if (cses.imd.country$IMD5000_F[i] == score){
    cses.imd.country$voted.for.score[i] <- cses.imd.country$IMD3007_F[i]
  } else if (cses.imd.country$IMD5000_G[i] == score){
    cses.imd.country$voted.for.score[i] <- cses.imd.country$IMD3007_G[i]
  } else if (cses.imd.country$IMD5000_H[i] == score){
    cses.imd.country$voted.for.score[i] <- cses.imd.country$IMD3007_H[i]
  } else if (cses.imd.country$IMD5000_I[i] == score){
    cses.imd.country$voted.for.score[i] <- cses.imd.country$IMD3007_I[i]
  }
}

# test
view(select(cses.imd.country, voted.for, IMD5000_B, IMD3007_B, voted.for.score))
summary(cses.imd.country$voted.for.score)
describe(cses.imd.country$voted.for.score)

cses.imd.country$voted.for.score[cses.imd.country$voted.for.score>10] <- NA
cses.imd.country <- cses.imd.country %>% drop_na(voted.for.score)

view(select(cses.imd.country, voted.for, voted.for.score, IMD3006)) # checking selfplacement influence
corr.test(cses.imd.country$voted.for.score, cses.imd.country$IMD3006)

# filling party.position var  
for (i in 1:nrow(cses.imd.country)){
  party <- cses.imd.country$voted.for[i]
  cses.imd.country$party.position[which(cses.imd.country$voted.for==party)] <- 
    mean(cses.imd.country$voted.for.score[which(cses.imd.country$voted.for==party)])
}
summary(cses.imd.country$party.position)

# 2 
### country mean position ###

cses.imd.country$country.mean.position <- NA
for (i in 1:nrow(cses.imd.country)){
  country <- cses.imd.country$IMD1004[i]
  cses.imd.country$country.mean.position[which(cses.imd.country$IMD1004==country)] <- 
    mean(cses.imd.country$party.position[which(cses.imd.country$IMD1004==country)])
}
summary(cses.imd.country$country.mean.position)

# 3 
### vote share ###

cses.imd.country$vote.share <- NA
for (i in 1:nrow(cses.imd.country)){
  vote <- cses.imd.country$voted.for[i]
  if (cses.imd.country$IMD5000_A[i] == vote){
    cses.imd.country$vote.share[i] <- cses.imd.country$IMD5001_A[i]
  } else if (cses.imd.country$IMD5000_B[i] == vote){
    cses.imd.country$vote.share[i] <- cses.imd.country$IMD5001_B[i]
  } else if (cses.imd.country$IMD5000_C[i] == vote){
    cses.imd.country$vote.share[i] <- cses.imd.country$IMD5001_C[i]
  } else if (cses.imd.country$IMD5000_D[i] == vote){
    cses.imd.country$vote.share[i] <- cses.imd.country$IMD5001_D[i]
  } else if (cses.imd.country$IMD5000_E[i] == vote){
    cses.imd.country$vote.share[i] <- cses.imd.country$IMD5001_E[i]
  } else if (cses.imd.country$IMD5000_F[i] == vote){
    cses.imd.country$vote.share[i] <- cses.imd.country$IMD5001_F[i]
  } else if (cses.imd.country$IMD5000_G[i] == vote){
    cses.imd.country$vote.share[i] <- cses.imd.country$IMD5001_G[i]
  } else if (cses.imd.country$IMD5000_H[i] == vote){
    cses.imd.country$vote.share[i] <- cses.imd.country$IMD5001_H[i]
  } else if (cses.imd.country$IMD5000_I[i] == vote){
    cses.imd.country$vote.share[i] <- cses.imd.country$IMD5001_I[i]
  }
}
summary(cses.imd.country$vote.share)
view(select(cses.imd.country, IMD1004, voted.for, vote.share))

cses.imd.country$vote.share[cses.imd.country$vote.share>100] <- NA
cses.imd.country <- cses.imd.country %>% drop_na(vote.share)


# 4
##########################
### party polarization ###
##########################

cses.imd.country$party.polarization <- NA
for (i in 1:nrow(cses.imd.country)){
  polarization <- cses.imd.country$IMD1004[i]
  cses.imd.country$party.polarization[which(cses.imd.country$IMD1004==polarization)] <- 
    sqrt(sum(
      cses.imd.country$vote.share[which(cses.imd.country$IMD1004==polarization)]/100 * # vote share
        ((cses.imd.country$party.position[which(cses.imd.country$IMD1004==polarization)] - # party position 
            cses.imd.country$country.mean.position[which(cses.imd.country$IMD1004==polarization)])/5)^2)) # country mean
}

summary(cses.imd.country$party.polarization)
view(unique(select(cses.imd.country, IMD1004, party.polarization)))

# alternative formula
cses.imd.country$party.polarization2 <- NA
for (i in 1:nrow(cses.imd.country)){
  polarization <- cses.imd.country$IMD1004[i]
  cses.imd.country$party.polarization2[which(cses.imd.country$IMD1004==polarization)] <- 
    sqrt(mean(
      (cses.imd.country$party.position[which(cses.imd.country$IMD1004==polarization)]) -     # party LR score
        cses.imd.country$country.mean.position[which(cses.imd.country$IMD1004==polarization)]^2* # country ave
        cses.imd.country$vote.share[which(cses.imd.country$IMD1004==polarization)]/100))
}

summary(cses.imd.country$party.polarization)
describe(cses.imd.country$party.polarization)
view(unique(select(cses.imd.country, IMD1004, party.polarization)))

###########################
### voters polarization ###
###########################

# same formula

# 1
### voter position
## the mean of voters' self placement for every party
cses.imd.country$voter.position <- NA

# self palcement
cses.imd.country$IMD3006[cses.imd.country$IMD3006>10] <- NA
cses.imd.country <- cses.imd.country %>% drop_na(IMD3006)

# filling voter position
for (i in 1:nrow(cses.imd.country)){
  voter <- cses.imd.country$voted.for[i]
  cses.imd.country$voter.position[which(cses.imd.country$voted.for==voter)] <- 
    mean(cses.imd.country$IMD3006[which(cses.imd.country$voted.for==voter)])
}
summary(cses.imd.country$voter.position)

# 2
### country mean

cses.imd.country$mean.voter.system <- NA
for (i in 1:nrow(cses.imd.country)){
  country <- cses.imd.country$IMD1004[i]
  cses.imd.country$mean.voter.system[which(cses.imd.country$IMD1004==country)] <- 
    mean(cses.imd.country$voter.position[which(cses.imd.country$IMD1004==country)])
}
summary(cses.imd.country$mean.voter.system)

###########################
### voters polarization ###
###########################

cses.imd.country$voter.polarization <- NA
for (i in 1:nrow(cses.imd.country)){
  voter.pol <- cses.imd.country$IMD1004[i]
  cses.imd.country$voter.polarization[which(cses.imd.country$IMD1004==voter.pol)] <- 
    sqrt(sum(
      cses.imd.country$vote.share[which(cses.imd.country$IMD1004==voter.pol)]/100 * # vote share
        ((cses.imd.country$voter.position[which(cses.imd.country$IMD1004==voter.pol)] - # voter position 
            cses.imd.country$mean.voter.system[which(cses.imd.country$IMD1004==voter.pol)])/5)^2)) # country mean
}

# alternative formula
cses.imd.country$voter.polarization2 <- NA
for (i in 1:nrow(cses.imd.country)){
  voter.pol <- cses.imd.country$IMD1004[i]
  cses.imd.country$voter.polarization2[which(cses.imd.country$IMD1004==voter.pol)] <- 
    sqrt(mean(
      (cses.imd.country$mean.voter.system[which(cses.imd.country$IMD1004==voter.pol)] -
         cses.imd.country$voter.position[which(cses.imd.country$IMD1004==voter.pol)])^2*
        cses.imd.country$vote.share[which(cses.imd.country$IMD1004==voter.pol)]/100))
}

summary(cses.imd.country$voter.polarization)
describe(cses.imd.country$voter.polarization)

view(unique(select(cses.imd.country, IMD1004, voter.polarization, party.polarization)))

setwd("C:/Users/yaira/Desktop/ideological polarization/FINAL/Tables and Graphs")

# creating df at the country level from the cses
country.pol <- data.frame(unique(cses.imd.country$IMD1004), unique(cses.imd.country$voter.polarization),
                          unique(cses.imd.country$party.polarization))
names(country.pol) <- c("Country", "voter.polarization.cses.imd", "party.polarization.cses.imd")


############## plot ##################

# 1 party polarization
CP <- 
  ggplot(cses.imd.country, aes(voter.polarization, party.polarization)) + 
  geom_point(size=2) +
  geom_smooth(method = lm, se = T, size = 0.1, fill="black", col= "grey70", level=1-1e-10) +
  geom_abline(intercept=0, slope=1) +
  xlim(0,10) + ylim (0,10) +
  labs(x = "Voter level polarization", y = "Party level polarization") +
  theme_bw() +
  theme(axis.title.x = element_text(size = 10),
        axis.title.y = element_text(size = 10),
        plot.title = element_text(size = 15),
        panel.border = element_blank(),
        axis.line = element_line(size = 0.5, linetype = "solid", colour = "black"))
CP
ggsave("voter-party polarization CSES.png", width = 7, height = 4)

# 1 party - voter extremity
cses.imd.country$party.extremity <- ((cses.imd.country$party.position - 
                                        cses.imd.country$country.mean.position)/5)^2
summary(cses.imd.country$party.extremity)
cses.imd.country$party.extremity[unique(cses.imd.country$IMD5000_A)]
hist(cses.imd.country$party.extremity)

cses.imd.country$voter.extremity <- ((cses.imd.country$voter.position - 
                                        cses.imd.country$mean.voter.system)/5)^2
summary(cses.imd.country$voter.extremity)
hist(cses.imd.country$voter.extremity)
#
linecolors <- c("#714C02", "#01587A", "#024E37")
fillcolors <- c("#9D6C06", "#077DAA", "#026D4E")
PP <- 
  ggplot(cses.imd.country, aes(voter.extremity, party.extremity)) + 
  geom_point(size = 2) +
  geom_smooth(method = lm, se = T, size = 0.1, fill="black", col= "grey70", level=1-1e-10) +
  xlim(0,1) + ylim (0,1) +
  geom_abline(intercept=0, slope=1) +
  labs(x = "Voter level extremity", y = "Party level extremity") +
  theme_bw() +
  theme(axis.title.x = element_text(size = 10),
        axis.title.y = element_text(size = 10),
        plot.title = element_text(size = 15),
        panel.border = element_blank(),
        axis.line = element_line(size = 0.5, linetype = "solid", colour = "black"))
PP
ggsave("voter-party extremity CSES.png", width = 7, height = 4)

ggarrange(CP, PP, nrow = 2, ncol = 1)

#########################################################################################################
############################################# (4) Figure 5  #############################################
#########################################################################################################

ggplot(ppdb.act, aes(factor(pfam), party.pol)) +
  geom_point(size = 2) +
  geom_point(data=isr, aes(x = factor(pfam), y = party.pol), 
             color='grey60', size=2) +
  geom_point(data=grb, aes(x = factor(pfam), y = party.pol), 
             color='grey80',  size=2) +
  labs(x = "", y = "Ideological incongruence") +
  geom_text(label = ppdb.act$labels.i.b, nudge_x = 0.25, check_overlap = T, size = 3.25) +
  geom_hline(yintercept=0, linetype="dashed", color = "black") +
  theme_bw() +
  theme(plot.title = element_text(size = 15), 
        axis.title.x = element_text(size = 12),
        axis.title.y = element_text(size = 10),
        panel.border = element_blank(),
        axis.line = element_line(size = 0.5, linetype = "solid", colour = "black")) +
  scale_x_discrete(breaks = 0:7, labels=c("Left\nSocialists","Greens","Social\nDemocrats",
                                          "Agrarian","Liberals","Christian\nDemocrats",
                                          "Conservative", "Far Right")) +
  scale_y_continuous(limits = c(-1.5, 1.5), breaks = c(-1, 0, 1),
                     labels = c("Right\ndeviation", "0", "Left\ndeviation"))
ggsave("ideological incongruence BW.png", width = 7, height = 5)

#########################################################################################################
############################################# (5) Figure 6  #############################################
#########################################################################################################
### ggploting party extremity
ppdb.fin$party.extremity2 <- ppdb.fin$party.extremity*-1

all2 <- 
  ggplot(ppdb.fin, aes( party.extremity2, activists)) + 
  geom_point(size = 2, col = "grey50") +
  ylim(c(0,1)) +
  geom_vline(xintercept= mean(ppdb.fin$party.extremity2), linetype = "dashed") +
  labs(x = "Party extremity", y = "Activists relative power", col = "Ideological\nposition") +
  scale_x_continuous(limits = c(-4.6, 4.5), breaks = c(-4.4, 0, 4.4),
                     labels = c("Extreme\nleft", "Moderate", "Extreme\nright")) +
  theme_bw() +
  theme(panel.border = element_blank(),
        axis.line = element_line(size = 0.5, linetype = "solid", colour = "black")) +
  theme(legend.position = "bottom")

all2

#########################################################################################################
############################################# (6) Figure 7  #############################################
#########################################################################################################

ggplot(ppdb.act, aes(factor(pfam), party.pol, label = out.pol2)) +
  geom_point(size = 2, aes(col = activists2)) +
  scale_color_distiller(type = "seq", direction = 1, palette = "Greys") +
  labs(x = "", y = "Ideological incongruence", col = "Activists'\nrelative\npower" ) +
  geom_hline(yintercept=0, linetype="dashed", color = "black") +
  geom_text(nudge_x = 0.35) +
  
  theme(panel.border = element_blank(),
        axis.line = element_line(size = 0.5, linetype = "solid", colour = "black")) +
  theme(axis.title.x = element_text(size = 12)) +
  theme(axis.title.y = element_text(size = 10)) +
  scale_x_discrete(breaks = 0:7, labels=c("Left\nSocialists","Greens","Social\nDemocrats",
                                          "Agrarian","Liberals","Christian\nDemocrats",
                                          "Conservative", "Far Right")) +
  scale_y_continuous(limits = c(-1.5, 1.5), breaks = c(-1, 0, 1),
                     labels = c("Right\ndeviation", "0", "Left\ndeviation"))
ggsave("activists2 on representaion BW.png", width = 7, height = 5)


#########################################################################################################
############################################# (8) Figure 9  #############################################
#########################################################################################################

## activists on party polarization (country level)
ggplot(ppdb.fin.coun, aes(coun.act, party.polarization, label = COUNTRY.YEAR)) + 
  geom_point(size = 2) +
  labs(x = "Activists' power (aggregated)", y = "Party polarization") +
  geom_smooth(method = lm, col = "grey40") +
  geom_text_repel(size = 3.5) +
  theme_bw() +
  theme(panel.border = element_blank(),
        axis.line = element_line(size = 0.5, linetype = "solid", colour = "black")) +
  theme(axis.title.x = element_text(size = 13)) +
  theme(axis.title.y = element_text(size = 13)) + 
  theme(plot.caption =  element_text(hjust = 0))
ggsave("activists on party polarization ppdb.fin BW.png", width = 7, height = 4.5)

##########################################################################################################################
